package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @Reference
    private HouseService houseService;
    @Reference
    private DictService dictService;
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseUserService houseUserService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @RequestMapping
    public String index(Map map, HttpServletRequest request){
        Map<String, Object> filters = getFilters(request);
        map.put("filters",filters);
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page",page);
        setRequestAttribute(map);
        return "house/index";
    }
//    去新增页面
    @RequestMapping("/create")
    public  String goAddPage(Map map){
        setRequestAttribute(map);
        return "house/create";
    }
//    新增
    @RequestMapping("/save")
    public String save(House house){
        houseService.insert(house);
        return "common/successPage";
    }
//    去修改页面
    @RequestMapping("/edit/{id}")
    public String goEditPage(@PathVariable("id")Long id, Map map){
        House house = houseService.getById(id);
        map.put("house",house);
        return "house/edit";
    }
//    更新
    @RequestMapping("/update")
    public String update(House house){
        houseService.update(house);
        return "common/successPage";
    }
//    删除
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        houseService.delete(id);
        return "redirect:/house";
    }
//    发布或者取消
    @RequestMapping("/publish/{houseId}/{status}")
    public String publish(@PathVariable("houseId") Long houseId,@PathVariable("status") Integer status){
        houseService.publish(houseId,status);
        return "redirect:/house";
    }
//    详情
    @RequestMapping("/{houseId}")
    public String show(@PathVariable("houseId") Long houseId,Map map){
        House house = houseService.getById(houseId);
        map.put("house",house);
        Community community = communityService.getById(house.getCommunityId());
        map.put("community",community);
        List<HouseImage> houseImage1List = houseImageService.getHouseImageByHouseIdAndType(houseId, 1);
        List<HouseImage> houseImage2List = houseImageService.getHouseImageByHouseIdAndType(houseId, 2);
        List<HouseBroker> houseBrokerList = houseBrokerService.getHouseBrokersByHouseId(houseId);
        List<HouseUser> houseUserList = houseUserService.getHouseUsersByHouseId(houseId);
        map.put("houseImage1List",houseImage1List);
        map.put("houseImage2List",houseImage2List);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseUserList",houseUserList);
        return "house/show";
    }


//    将所有小区及数据字典中数据放到request域中的方法
    public void setRequestAttribute(Map map){
        //       获取小区
        List<Community> communityList = communityService.findAll();
//        获取户型
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
//        获取楼层
        List<Dict> floorList = dictService.findListByDictCode("floor");
//        获取建筑结构
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
//        朝向
        List<Dict> directionList = dictService.findListByDictCode("direction");
//        装修情况
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
//        房屋用途
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        map.put("communityList",communityList);
        map.put("houseTypeList",houseTypeList);
        map.put("floorList",floorList);
        map.put("buildStructureList",buildStructureList);
        map.put("directionList",directionList);
        map.put("decorationList",decorationList);
        map.put("houseUseList",houseUseList);
    }

}
